package com.onegroup.service.impl;

import com.onegroup.mapper.IndustryTypeMapper;
import com.onegroup.mapper.ProjectMapper;
import com.onegroup.pojo.IndustryType;
import com.onegroup.pojo.Project;
import com.onegroup.service.DataAnalysisService;
import com.onegroup.common.utils.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 林秦发
 * @Description:数据分析服务层实现类
 * @History:
 */
@Service
public class DataAnalysisServiceImpl implements DataAnalysisService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private IndustryTypeMapper industryTypeMapper;

    //展示所有项目
    @Override
    @Transactional(readOnly = true)
    public List<Project> showAllProjects(String proType) {
        List<Project> allProjects = projectMapper.getAllProjects();
        return allProjects;
    }

    //得到所有项目类型以及相应项目类型下的项目数量
    @Override
    @Transactional(readOnly = true)
    public List<IndustryType> getProjectTypeAndQuantity() {
        List<IndustryType> proTypeAndQuantity = industryTypeMapper.getProTypeAndQuantity();
        System.out.println(proTypeAndQuantity.size());//共16个行业类型
        //开始都是0
        Integer quantity = 0;
        for (IndustryType industryType : proTypeAndQuantity) {
            industryType.setQuantity(quantity);
        }
        List<Project> allProjects = projectMapper.getAllProjects();
        Integer typeId = 0;
        for (Project allProject : allProjects) {
            //获取每一个项目的项目行业类型id
            typeId = allProject.getIndustryType().getTypeId();
            //对上面的行业类型数量加1
            for (IndustryType industryType : proTypeAndQuantity) {
                if (industryType.getTypeId().equals(typeId)) {
                    industryType.setQuantity(quantity + 1);
                }
            }
        }
        return proTypeAndQuantity;
    }

    //给当前展示的项目列表导出excel
    @Override
    @Transactional
    public void exportExcelOfCurrentProjectList
    (HttpServletRequest request, HttpServletResponse response) {
        List<Project> pros = new ArrayList<>();
        //创建excel表头
        List<String> column = new ArrayList<>();
        //表头对应的数据
        List<Map<String, Object>> data = new ArrayList<>();
        pros = projectMapper.getAllProjects();
        //创建excel表头
        column.add("项目序号");
        column.add("项目名字");
        //遍历获取到的需要导出的数据，k要和表头一样
        for (int i = 0; i < pros.size(); i++) {
            Map<String, Object> dataMap = new HashMap<>();
            Project pro = pros.get(i);
            dataMap.put("项目序号", pro.getProId());
            dataMap.put("项目名字", pro.getProName());
            data.add(dataMap);
        }
        //调用导出工具类
        ExportExcel.exportExcel("项目数据数据", column, data, request, response);
    }
}
